import { Injectable } from '@nestjs/common';
import { Messages } from 'protobuf/Messages';
import { User } from 'src/class/user';
import { CodeEnum } from 'src/enum/CodeEnum';
import { userManager } from 'src/manager/user-manager';

@Injectable()
export class LoginService {

    public login(request: Messages.LoginRequest)
    {
        let user = userManager.getUserByPsw(request.userName, request.psw);
        if (user == null) {
            user = new User();
            user.userName = request.userName;
            user.psw = request.psw;
            user.roomId = 0;
            user.uid = userManager.createNewUid();
            userManager.registerUser(user);
        }

        let response: Messages.ILoginRespone = {
            code: CodeEnum.success,
            uid: user.uid,
        };

        return response;
    }
}
